package ctrLayer;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import modelLayer.*;

public class FlavourCtr {
	private FlavourContainer fCont;
	
	public FlavourCtr(){
		fCont = FlavourContainer.getInstance();
	}
	
	public void addFlavour(String flavourName){
		if(fCont.getFlavour(flavourName) == null){
			fCont.addFlavour(new Flavour(flavourName));
		}else{
			throw new NullPointerException("The Flavour already exists!");
		}
	}
	
	public void toggleLimited(String flavourName){
		Flavour f = fCont.getFlavour(flavourName);
		if(f != null){
			f.toggleLimited();
		}else{
			throw new NullPointerException("The Flavour does not exist!");
		}
	}
	
	public String getFlavour(String flavourName){
		Flavour f = fCont.getFlavour(flavourName);
		if(f != null){
			String ret = "Flavour: " + f.getFlavour() + " - Bottles: " +f.getBottles();
			if(f.isLimited()){
				ret += " - Limited";
			}
			return ret;
		}else{
			throw new NullPointerException("The Flavour does not exists!");
		}
	}
	
	public int getBottles(String flavourName){
		Flavour f = fCont.getFlavour(flavourName);
		if(f != null){
			int ret = f.getBottles();
			return ret;
		}else{
			throw new NullPointerException("The Flavour does not exists!");
		}
	}
	
	public void changeBottles(String flavourName, int bottles){
		Flavour f = fCont.getFlavour(flavourName);
		if(f == null){
			fCont.addFlavour(new Flavour(flavourName));
			f = fCont.getFlavour(flavourName);
		}
		f.setBottles(f.getBottles() + bottles);
	}
	
	public String printAll(){
		ArrayList<Flavour> flavours = fCont.getAll();
		if(flavours != null){
			String ret = "";
			String nL = System.getProperty("line.separator");
			for(Flavour f : flavours){
				ret += getFlavour(f.getFlavour()) + nL;
				if(f.hasSales() == 1){
					ret += " - Sales" + nL;
					ArrayList<Sale> sales = f.getSales();
					for(Sale s : sales){
						ret += "  - " + s.getId() + " ~ " + s.getAmount() + " ~ " + s.getDate() + nL;
					}
				}
			}
			return ret;
		}else{
			throw new NullPointerException("There is not Flavours added yet!");
		}
	}
	
	public void addSale(String flavourName, int amount){
		Flavour f = fCont.getFlavour(flavourName);
		if(f == null){
			fCont.addFlavour(new Flavour(flavourName));
			f = fCont.getFlavour(flavourName);
		}
		f.addSales(amount);
	}
	
	public void load() throws IOException, ParseException{
		BufferedReader bf = new BufferedReader(new FileReader(new File("./flavour.txt")));
		String line;
		while((line = bf.readLine()) != null){
			if(!line.equals("") && !line.equals(" ") && !line.isEmpty()){
				String[] sp = line.split(",");
				Integer id = Integer.valueOf(sp[0]);
				
				String flavour = sp[1];
				Integer bottle = Integer.valueOf(sp[2]);
				Integer hasSales = Integer.valueOf(sp[3]);
				Integer limited = Integer.valueOf(sp[4]);
				if(id != null && flavour != null && bottle != null){
					fCont.addFlavour(new Flavour(id, flavour, bottle, limited));
					if(hasSales == 1){
						loadSales(id, flavour);
					}
				}
			}
		}
		bf.close();
	}
	
	public void loadSales(int id, String flavourName) throws IOException, ParseException{
		BufferedReader bf = new BufferedReader(new FileReader(new File("./" + id + ".txt")));
		String line;
		while((line = bf.readLine()) != null){
			if(!line.equals("") && !line.equals(" ") && !line.isEmpty()){
				String[] sp = line.split(",");
				
				Integer salesId = Integer.valueOf(sp[0]);
				Integer amount = Integer.valueOf(sp[1]);
				String d = sp[2];
				Date salesDate = new SimpleDateFormat("dd/MM/yyyy").parse(d);
				if(salesId != null && amount != null && !d.isEmpty()){
					Flavour f = fCont.getFlavour(flavourName);
					f.addSales(salesId, amount, salesDate);
				}
			}
		}
	}
	
	public void save() throws IOException{
		Writer writer = null;
		String nL = System.getProperty("line.separator");
		ArrayList<Flavour> flavours = fCont.getAll();
		if(flavours != null){
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("./flavour.txt"), "UTF-8"));
			for(Flavour f : flavours){
				int limit = 0;
				if(f.isLimited()){
					limit = 1;
				}
				writer.write(f.getId() + "," + f.getFlavour() + "," + f.getBottles() + "," + f.hasSales() + "," + limit + nL);
				ArrayList<Sale> sales = f.getSales();
				if(sales != null){
					Writer sWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("./" + f.getId() + ".txt"), "UTF-8"));
					for(Sale s : sales){
						sWriter.write(s.getId() + "," + s.getAmount() + "," + s.getDate() + nL);
					}
					sWriter.close();
				}
			}
			writer.close();
		}
	}
	
	public boolean saveExist(){
		boolean ret = false;
		File f = new File("flavour.txt");
		if(f.exists() && !f.isDirectory()){
			ret = true;
		}
		return ret;
	}

	public Flavour getFlavourObj(String flavourName) {
		Flavour f = fCont.getFlavour(flavourName);
		if(f != null){
			return f;
		}else {
			return null;
		}
	}

}
